//
//  HJCityPicker.m
//  Sneakers
//
//  Created by 胡光健 on 2017/10/19.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJCityPicker.h"
@interface HJCityPicker ()<UIPickerViewDelegate,UIPickerViewDataSource>{
    NSString *_city;
    NSString *_county;
    NSString *_district;
}

@property(nonatomic,strong) NSArray * dicArr;
@property(nonatomic,strong) NSMutableArray * CitysArr;

@property(nonatomic,strong) NSArray * countyArr;
@property(nonatomic,strong) NSArray * districtArr;
@end

@implementation HJCityPicker
- (void)awakeFromNib {
    [super awakeFromNib];
    self.cityPicker.delegate = self;
    self.cityPicker.dataSource = self;
    [self.cityPicker setValue:[UIColor blackColor] forKey:@"textColor"];
}

-(instancetype)init {
    if (self = [super init]) {
        self = [[NSBundle mainBundle]loadNibNamed:@"HJCityPicker" owner:self options:nil].firstObject;
        self.dicArr = [[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForAuxiliaryExecutable:@"cityData.plist"]]mutableCopy];
        
        self.countyArr = self.dicArr[0][@"childArea"];
        self.districtArr = self.countyArr[0][@"childArea"];
        
        _city = self.dicArr[0][@"name"];
        _county = self.countyArr[0][@"name"];
        _district = self.districtArr[0][@"name"];
    }
    return self;
}

- (IBAction)colseBtn:(UIButton *)sender {
    NSString *str = [NSString stringWithFormat:@"%@ %@ %@",_city,_county, _district];
    if ([self.delegate respondsToSelector:@selector(PickerViewDidClickOKButton:)]) {
        [self.delegate PickerViewDidClickOKButton:str];
    }
    
}
#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
    
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    switch (component) {
        case 0: { return self.dicArr.count; } break;
        case 1: { return self.countyArr.count; } break;
        case 2: { return self.districtArr.count; } break;
        default: return 0;  break;
    }
}


- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view {
    UILabel *label;
    if (view) {
        label = (UILabel *)view;
    } else {
        label = [UILabel new];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor =[UIColor colorWithDisplayP3Red:74/255.0 green:74/255.0 blue:74/255.0 alpha:1];
    }
    
    switch (component) {
        case 0:
            label.text = self.dicArr[row][@"name"];
            break;
        case 1:
            if (self.countyArr.count > row) {
                label.text = self.countyArr[row][@"name"];
            }
            break;
        case 2:
            if (self.districtArr.count > row) {
                label.text = self.districtArr[row][@"name"];
            }
            break;
        default:
            break;
    }
    
    if (label.text.length > 5) {
        label.font = HJFontSize(16);
    }else if (label.text.length > 4) {
        label.font = HJFontSize(17);
    }else {
        label.font = HJFontSize(18);
    }
    
    return label;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    switch (component) {
        case 0: {
            self.countyArr = self.dicArr[row][@"childArea"];
            self.districtArr = self.countyArr[0][@"childArea"];
            _city = self.dicArr[row][@"name"];
            _county = self.countyArr[0][@"name"];
            _district = self.districtArr[0][@"name"];
            [pickerView reloadAllComponents];
        } break;
        case 1: {
            self.districtArr = self.countyArr[row][@"childArea"];
            _county = self.countyArr[row][@"name"];
            _district = self.districtArr[0][@"name"];
            [pickerView reloadComponent:2];
            
        } break;
        case 2: {
            _district = self.districtArr[row][@"name"];
            
        } break;
            
        default: break;
    }
    
}
@end
